package com.nononsenseapps.feeder.ui.tag

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.gitee.wsl.struct.category.CategoryItemWarp
import com.kongzue.stacklabelview.StackLabel
import com.nononsenseapps.feeder.db.entity.ATTENTION_NORMAL
import com.nononsenseapps.feeder.ui.R
import timber.log.Timber

class ItemTagHolder(val v: View, private val itemTagCallBack: ItemTagClickCallBack,val repository: ItemTagRepository): androidx.recyclerview.widget.RecyclerView.ViewHolder(v), View.OnClickListener {

    val itemTagName : TextView =v.findViewById(R.id.item_tag_name)
    private val itemTagSubList : StackLabel =v.findViewById(R.id.item_tag_sub_list)
    private val itemTagNum : TextView =v.findViewById(R.id.item_tag_num)
    val expander: ImageView = v.findViewById(R.id.item_tag_expander)

    lateinit var baseItem : CategoryItemWarp

    val showItemTagList = ArrayList<CategoryItemWarp>()

    init {
        itemTagName.setOnClickListener {
            if(!baseItem.isRoot||!repository.isEditModel())
                itemTagCallBack.onItemTagClick(baseItem)
        }
        itemTagName.setOnLongClickListener {
            if(repository.isEditModel())
                showFeedItemTagPopMenu(v.context,it,baseItem,repository)
            true
        }

        itemTagSubList.setOnLabelClickListener { index, v, s ->
            val selectTag=showItemTagList[index]
            if( !selectTag.isLeaf ) {
                if(selectTag.isExpand)
                    itemTagCallBack.onItemTagClick(selectTag)
                else{
                    selectTag.toggle()
                    changeShowItemTagList()
                }
            }else{
                itemTagCallBack.onItemTagClick(selectTag)
            }

        }

        itemTagSubList.setOnLabelLongClickListener { index, v, _ ->
            val itemTag = showItemTagList[index]
            if(!itemTag.isLeaf && itemTag.isExpand){
                unExpanderItem(itemTag)
                changeShowItemTagList()
            }else{
                if(repository.isEditModel())
                    showFeedItemTagPopMenu(v.context,v,itemTag,repository)
                else
                    itemTagCallBack.onItemTagLongClick(itemTag,v)
            }

        }

        expander.setOnClickListener {
            baseItem.toggle()
            changeShowItemTagList()
        }

        itemTagNum.setOnClickListener {
            baseItem.toggle()
            changeShowItemTagList()
        }
    }

    fun updateItem(item : CategoryItemWarp){
        this.baseItem=item
        itemTagName.text = "♆. "+repository.getItemWarpTitle(item)
        itemTagNum.text = item.subItemList.size.toString()
        changeShowItemTagList()
    }

    fun changeShowItemTagList(){
        showItemTagList.clear()

        addItemTagList(baseItem)

        if(baseItem.isExpand) {
            expander.setImageResource(R.drawable.tinted_expand_more)
        }else {
            if(showItemTagList.size == 0){
                showItemTagList.addAll(baseItem.leafList().take(3).map { it as CategoryItemWarp})
            }
            expander.setImageResource(R.drawable.tinted_expand_less)
        }

        Timber.d("call in ItemTagsFilterDrawerPopup  changeShowItemTagList show item count : %d leafList count :%d",showItemTagList.size,baseItem.leafList().size)

        itemTagSubList.setLabels(showItemTagList.map {
            val name=repository.getItemWarpTitle(it)
            if(it.isLeaf)
               name
            else "♆.${name}"
        }.toList())

        itemTagSubList.visibility= if( showItemTagList.size == 0 ) View.GONE else View.VISIBLE
    }

    private fun expanderItem(item : CategoryItemWarp){

    }

    private fun unExpanderItem(item : CategoryItemWarp){
        item.shrink()
        item.subItemList.forEach { unExpanderItem(it) }
    }

    fun addItemTagList(item : CategoryItemWarp){
        val addItemList=if( item.isExpand )
            item.subItemList
         else
            item.subItemList.filter { repository.getAttentionLevel(it) > ATTENTION_NORMAL }

        addItemList.forEach {
            showItemTagList.add(it)
            if(!it.isLeaf)
                addItemTagList(it)
        }
    }

    override fun onClick(v: View?) {
        itemTagCallBack.onItemTagClick(baseItem)
    }
}